<?php   
/**
    *   @authorPeterDoo   
    *   @version1.0   
    */   
class   swf   {
	var   $swf   =   null;//   swf文件指针
	var   $swfHeader   =   null;//   swf文件头
	var   $compress   =   "Unknow";//   swf文件压缩状态
	var   $version   =   "Unknow";//   swf文件版本
	var   $size   =   "Unknow";//   swf文件大小
	var   $frameRate   =   "Unknow";//   swf文件帧频
	var   $frameTotal   =   "Unknow";//   swf文件帧数

	/**
    *   构造函数   
    *   @param   string   $file   swf文件名   
    */   
	function   swf($file   =   '')   {
		if   (!empty($file))   {
			$this->initSWFHeader($file);
		}
	}

	/**
    *   初始化解析器swf文件头   
    *   @param   string   $file   swf文件名   
    *   @return   boolean   
    *   @access   private   
    */   
	function   initSWFHeader($file)   {
		//$fmt   =   strtolower(substr($file,   strpos($file,   '.')   +   1));
		$fmt = explode('.',$file);
		$fmt = end($fmt);

		if   ($fmt   ==   'swf')   {
			$this->swf   =   @fopen($file,   'rb');
			//$this->swf   =   @file_get_contents($file,   'rb');

			if   (!$this->swf)   {
				return false;
			}

			$sign   =   fread($this->swf,   3);

			if   (!($sign   ==   "FWS"   ||   $sign   ==   "CWS"))   {
				@fclose($this->swf);
				return false;
			}   else   {
				if   ($sign[0]   ==   'C')   {
					if   (function_exists("gzuncompress"))   {
						fseek($this->swf,   4);

						$len   =   fread($this->swf,   4);
						$len   =   strrev($len);
						$len   =   hexdec(bin2hex($len));

						$buffer   =   fread($this->swf,   $len);
						$buffer   =   gzuncompress($buffer,   $len);

						fseek($this->swf,   0);

						$header   =   fread($this->swf,   8);
						$header   .=   substr($buffer,   0,   20);
						
						unset($buffer);
						$this->swfHeader   =   $header;
						unset($header);
					}   else   {
						@fclose($this->swf);
						return false;
					}
				}   else   if   ($sign[0]   ==   'F')   {
					fseek($this->swf,   0);
					$this->swfHeader   =   fread($this->swf,   28);
				}   else   {
					@fclose($this->swf);
					return false;
				}

				@fclose($this->swf);
				return   true;
			}
		}   else   {
			return false;
		}
	}

	/**
    *   获取swf文件压缩标识   
    *   @return   string   
    */   
	function   getCompressFlag()   {
		switch   ($this->swfHeader[0])   {
			case   'F':
				$this->compress   =   "Uncompressed";
				break;
			case   'C':
				$this->compress   =   "Compressed";
				break;
		}

		return   $this->compress;
	}

	/**
    *   获取swf文件版本   
    *   @return   number   
    */   
	function   getVersion()   {
		$this->version   =   ord($this->swfHeader[3]);
		return   $this->version;
	}

	/**
    *   获取swf文件大小   
    *   @return   string   
    */   
	function   getSize()   {
		$sum   =   strrev(substr($this->swfHeader,   4,   4));

		$sum   =   hexdec(bin2hex($sum));

		$this->size   =   $sum   .   '   bytes(Uncompressed)';

		return   $this->size;
	}

	/**
    *   获取swf文件头中RECT结构结束位置   
    *   @return   number   
    */   
	function   getRECTEnd()   {
		$Nbits   =   ord($this->swfHeader[8]);

		$Nbits   >>=   3;

		$len   =   ceil((($Nbits   *   4)   -   3)   /   8);

		$offset   =   8   +   $len;

		return   $offset;
	}

	/**
    *   获取播放窗口大小   
    *   @return   array   第一元素表示宽,第二元素表示高   
    */   
	function   getScreen()   {
		$Nbits   =   ord($this->swfHeader[8]);

		$Nbits   >>=   3;

		$seg_len   =   $this->getRECTEnd()   -   8;

		$str   =   substr($this->swfHeader,   8,   $seg_len);

		$str_len   =   strlen($str);

		$bin_str   =   '';

		for   ($i   =   0;   $i   <   $str_len;   $i++)   {
			for   ($j   =   7;   $j   >=   0;   $j--)   {
				$cur_byte   =   ord($str[$i]);
				$cur_byte   >>=   $j;
				$bin_str   .=   ($cur_byte   &   1);
			}
		}

		for   ($i   =   0;   $i   <   4;   $i++)   {
			$cur_bitval   =   substr($bin_str,   5   +   $Nbits   *   $i,   $Nbits);
			$val   =   0;

			for   ($j   =   0;   $j   <   $Nbits;   $j++)   {
				$s   =   0;

				if   ((int)$cur_bitval[$j])   {
					$s   =   1;

					for   ($k   =   0;$k   <   $Nbits   -   $j   -   1;   $k++)   {
						$s   *=   2;
					}
				}

				$val   +=   $s;
			}

			$tmp[$i]   =   $val   /   20;
		}

		$screen[0]   =   $tmp[1]   -   $tmp[0];
		$screen[1]   =   $tmp[3]   -   $tmp[2];

		return   $screen;
	}

	/**
    *   获取swf文件帧频   
    *   @return   string   
    */   
	function   getFrameRate()   {
		$offset   =   $this->getRECTEnd()   +   1;

		$rate   =   strrev(substr($this->swfHeader,   $offset,   2));

		$r1   =   ord($rate[0]);

		$r2   =   bin2hex($rate[1]);

		$r2   =   ($r2[0]   /   16)   +   ($rs[1]   /   (16   *   16));

		$rate   =   $r1   +   $r2;

		$this->frameRate   =   $rate   .   "fps";

		return   $this->frameRate;
	}

	/**
    *   获取swf文件帧数   
    *   @return   number   
    */   
	function   getFrameTotal()   {
		$offset   =   $this->getRECTEnd()   +   1;

		$total   =   strrev(substr($this->swfHeader,   $offset   +   2,   2));

		$total   =   hexdec(bin2hex($total));

		$this->frameTotal   =   $total;

		return   $this->frameTotal;
	}
}
  ?>   